import { request } from '../../../request/index'

Page({
  data: {
    t_user:"",
    username: "",
    password: "",
    nickname:'',
    address:''
  },

  onLoad: function(e) {
    let tuser = wx.getStorageSync('user')
    if (!tuser) {
      wx.navigateTo({url: '../login/login'})
      return
    }
    console.log(tuser)   
    request({
        url: '/user/' + tuser.id ,
        method: 'GET'
      }).then(res => {
        var t=this
        console.log(tuser)
        this.setData({
          t_user: tuser,
          username:tuser.username,
          password:tuser.password,
          nickname:tuser.nickname,
          address:tuser.address
        })
        console.log(this.data.t_user)
        console.log(this.data.t_user.username)
      }) 
  },

  modify() {
    let tuser = wx.getStorageSync('user')
    let data = {
      username: this.data.username,
       password: this.data.password,
       nickname:this.data.nickname,
       address:this.data.address,
      }
    request({url: "/user/modify/" + tuser.id, data: data, method: 'POST'}).then(res => {
        // 请求成功时的回调
        if (res) {
          // 成功时的操作
          wx.showToast({
            title: '修改成功',
            icon: 'succcess'
          })
          wx.setStorageSync('user', res)
  
          setTimeout(() => {
            wx.switchTab({
              url: '../me',
            })
          }, 2000)
        } else {
          wx.showToast({
            title: '修改错误',
            icon: 'error'
          })
        }
      })
  }
})